Page({

  /**
   * 页面的初始数据
   */
  data: {
    url: getApp().globalData.url,
    username: "",
    //显示数据
    doctor: [],
    scrollHeight: 0,
    currentId: -1,
    doctor1: [{
      logoLink: '/images/defaultImage.png',
      doctorName: '张峻宇张峻宇张峻宇张峻宇',
      position: '主治医师',
      department: "口腔科",
      tel: "18727508188"
    },
    {
      logoLink: '/images/defaultImage.png',
      doctorName: '徐静茹',
      position: '副主治医师',
      department: "儿童口腔科",
      tel: "19909722365"
    },
    {
      logoLink: '/images/defaultImage.png',
      doctorName: '张峻宇张峻宇张峻宇张峻宇',
      position: '主治医师',
      department: "口腔科",
      tel: "18727508188"
    },
    {
      logoLink: '/images/defaultImage.png',
      doctorName: '徐静茹',
      position: '副主治医师',
      department: "儿童口腔科",
      tel: "19909722365"
    },
    {
      logoLink: '/images/defaultImage.png',
      doctorName: '张峻宇张峻宇张峻宇张峻宇',
      position: '主治医师',
      department: "口腔科",
      tel: "18727508188"
    },
    {
      logoLink: '/images/defaultImage.png',
      doctorName: '徐静茹',
      position: '副主治医师',
      department: "儿童口腔科",
      tel: "19909722365"
    }
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.stopPullDownRefresh();

    this.getDoctor();
    this.enterPage(9);

    var that = this;
    //动态scrollview
    let windowHeight = wx.getSystemInfoSync().windowHeight // 屏幕的高度
    let windowWidth = wx.getSystemInfoSync().windowWidth // 屏幕的宽度
    this.setData({
      scrollHeight: (windowHeight - 0 - 60) * 750 / windowWidth
    })
  },

  /**
   * 进入appointments页面，通知后端将进入此页面次数加一
   */
  enterPage: function (e) {
    var that = this
    wx.request({
      url: getApp().globalData.url + 'patientsettablebar',
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        unitphone: getApp().globalData.username,
        pageNum: e
      },
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      username: getApp().globalData.username,
    })
    this.getDoctor()
    try {
      var ap = wx.getStorageSync('doctor')
      this.setData({
        doctor: ap
      })
    } catch(e) { }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getDoctor()
  },

  /**
   * 获取医生信息
   */
  getDoctor: function () {
    var that = this
    wx.request({
      url: getApp().globalData.url + 'getDoctorServlet',
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        tel: getApp().globalData.username
      },
      success: function (res) {
        if (res.data != false && res.data != "false" && res.data!="空") {
          try {
            wx.setStorageSync("doctor", res.data)
          } catch (e) { }
          that.setData({
            doctor: res.data
          })
        }
        else if (res.data == "空"){
          that.setData({
            doctor: null
          })
        }
        else {
          wx.showToast({
            title: '获取医生列表失败，请从顶部下拉刷新',
            icon: 'none',
            duration: 2000
          })
        }
      },
      fail(res) {
        wx.showToast({
          title: '获取失败，请从顶部下拉刷新',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },

  /**
   * 滑动事件
   */
  backToStart: function(e) {
    if (this.data.currentId != e.currentTarget.dataset.idx && this.data.currentId != -1) {
      this.setData({
        scl: 0
      })
    }
    this.setData({
      currentId: e.currentTarget.dataset.idx
    })
  },

  /**
   * 跳转预约此医生的患者列表
   */
  clickDoctor: function(e) {
    var that = this
    //拿到点击的index下标
    var index = e.currentTarget.dataset.idx
    //将对象转为string
    var queryBean = JSON.stringify(that.data.doctor[index].tel)
    wx.navigateTo({
      url: '../appointments/appointments?doctorTel=' + queryBean,
    })
  },

  /**
   * 跳转聊天界面
   */
  consult: function(e) {
    var that = this
    //拿到点击的index下标
    var index = e.currentTarget.dataset.idx
    var otherTel = JSON.stringify(that.data.doctor[index].tel)
    var otherName = JSON.stringify(that.data.doctor[index].doctorName)
    var T = JSON.stringify(true)
    var F = JSON.stringify(false)
    wx.navigateTo({
      url: '../onlineConsulting/onlineConsulting?otherTel=' + otherTel + '&&otherName=' + otherName + '&&kefu=' + F + '&&doctorTel=' + otherTel + '&&isDoctor=' + T,
    })
  }
})